package com.rizzo.web.security;

import java.util.NoSuchElementException;

/**
 * An enum that designates the possible roles that can be assigned to a logged in user.
 *
 * @author jalieven
 */
public enum RoleEnum {

    anonymous("anonymous", "ROLE_"),
    superuser("superuser", "ROLE_");

    private String roleName;
    private String prefix;

    /**
     * Default constructor.
     *
     * @param roleName
     * @param prefix
     */
    private RoleEnum(String roleName, String prefix) {
        this.roleName = roleName;
        this.prefix = prefix;
    }

    /**
     * Setter for property 'roleName'.
     *
     * @param roleName Value to set for property 'roleName'.
     */ // Getters and setters
    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    /**
     * Getter for property 'roleName'.
     *
     * @return Value for property 'roleName'.
     */
    public String getRoleName() {
        return roleName;
    }

    /**
     * Getter for property 'prefix'.
     *
     * @return Value for property 'prefix'.
     */
    public String getPrefix() {
        return prefix;
    }

    /**
     * Setter for property 'prefix'.
     *
     * @param prefix Value to set for property 'prefix'.
     */
    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    /**
     * Getter for property 'fullRoleName' i.e. the prefix + roleName.
     *
     * @return Value for property 'fullRoleName'.
     */
    public String getFullRoleName() {
        return this.prefix + this.roleName;
    }

    /**
     * Returns the enum for a given roleName.
     *
     * @param roleName The roleName for which we want the enum (incl prefix).
     * @return A found RoleEnum.
     * @throws java.util.NoSuchElementException
     *          If none is found.
     */
    public static RoleEnum getEnumForAlias(String roleName) throws NoSuchElementException {
        RoleEnum[] roleEnums = RoleEnum.values();
        for (RoleEnum roleEnum : roleEnums) {
            if (roleEnum.getFullRoleName().equals(roleName)) {
                return roleEnum;
            }
        }
        throw new NoSuchElementException("No RoleEnum found for roleName : " + roleName);
    }
}